using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;
using System;

namespace RMC
{
	public class Application : ModelItem
	{
		private AddInHost host;
		private int port = 10000;
		TcpServer tcpServer = null;
		TcpInfoServer tcpInfoServer = null;

		public Application(AddInHost host)
		{
			this.host = host;
		}

		public MediaCenterEnvironment MediaCenterEnvironment
		{
			get
			{
				if (host == null) return null;
				return host.MediaCenterEnvironment;
			}
		}

		public void Start()
		{
			System.Diagnostics.Debugger.Log(0, "RMC", "\nStart modelitem....\n");
			try
			{
				//System.Threading.Thread.CurrentThread.Priority = ThreadPriority.Lowest;

				//tcpServer = new TcpServer(port);
				tcpInfoServer = new TcpInfoServer(port + 1000);

				tcpServer.Start();
				tcpInfoServer.Start();

				//waitHandle.WaitOne();

				//tcpServer.Stop();
				//tcpInfoServer.Stop();
			}
			catch (Exception ex)
			{
				System.Diagnostics.Debugger.Log(0, "RMC", "\n" + ex.ToString() + "\n");
			}
		}

		public void Stop()
		{
			//tcpServer.Stop();
			tcpInfoServer.Stop();
		}

		public void DialogTest(string strClickedText)
		{
			int timeout = 5;
			bool modal = true;
			string caption = Resources.DialogCaption;

			if (host != null)
			{
				MediaCenterEnvironment.Dialog(strClickedText,
																			caption,
																			new object[] { DialogButtons.Ok },
																			timeout,
																			modal,
																			null,
																			delegate(DialogResult dialogResult) { });
			}
			else
			{
				Debug.WriteLine("DialogTest");
			}
		}
	}
}